package workerwithdb.dao;



import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import api.entitites.Entities;

public abstract class GenericDAOHibernate<T extends Entities> {
	
	private Class<T> currentClass;
	
	
	protected GenericDAOHibernate(Class<T> cl){
		this.currentClass=cl;
	}
	
	

	public void update(Entities o,Session session) throws HibernateException{
		session.update(o);
	}

	public void save(Entities e,Session session) throws HibernateException {
		session.save(e);		
	}

	public void delete(Entities o,Session session) throws HibernateException {
		session.delete(o);
	}

	public void saveOrUpdate(Entities e,Session session){
		session.saveOrUpdate(e);
	}
	
	public Entities get(String propertyName, Object value,Session session) throws HibernateException {
		Criteria criteria = session.createCriteria(currentClass).add(Restrictions.eq(propertyName, value));
		Entities entity = (Entities) criteria.uniqueResult();
		return entity;		
	}
	
	@SuppressWarnings("unchecked")
	public List<T> getAll(Session session) throws HibernateException {
		Criteria criteria = session.createCriteria(currentClass);
		List<T> list = criteria.list();
		return list;
	}
	
}
